package com.cbs.android.component.diskcache;

import android.content.Context;

import com.cbs.android.component.fileutils.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by broche on 7/13/16.
 */
public final class DiskCache {
    private static final String TAG = DiskCache.class.getName();

    private DiskCache() {
    }

    public static long getCacheSize(List<String> cachePaths) {
        long size = 0;
        for (String path : cachePaths) {
            File file = new File(path);
            size += FileUtils.getFolderSize(file);
        }
        return size;
    }

    public static String getFormattedCacheSize(List<String> cachePaths) {
        return FileUtils.getFormattedSize(getCacheSize(cachePaths));
    }

    public static long getCacheSize(Context context) {
        List<String> cachePaths = new ArrayList<>();
        if (context.getCacheDir() != null) {
            cachePaths.add(context.getCacheDir().getPath());
        }
        if (context.getExternalCacheDir() != null) {
            cachePaths.add(context.getExternalCacheDir().getPath());
        }
        return getCacheSize(cachePaths);
    }

    public static String getFormattedCacheSize(Context context) {
        return FileUtils.getFormattedSize(getCacheSize(context));
    }

    public static void clearCache(List<String> cachePaths) {
        for (String path : cachePaths) {
            File file = new File(path);
            FileUtils.deleteFolder(file, false);
        }
    }

    public static void clearCache(Context context) {
        List<String> cachePaths = new ArrayList<>();
        if (context.getCacheDir() != null) {
            cachePaths.add(context.getCacheDir().getPath());
        }
        if (context.getExternalCacheDir() != null) {
            cachePaths.add(context.getExternalCacheDir().getPath());
        }
        clearCache(cachePaths);
    }
}
